package qdu.rgzb23.ch6_miscs;


//import android.app.FragmentManager;
//import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class Mymain extends FragmentActivity /*implements View.OnClickListener*/{
	 //UI Object
    private TextView txt_topbar;
    private TextView txt_channel;
    private TextView txt_message;
    private TextView txt_better;
    private TextView txt_setting;
    private FrameLayout ly_content;
    //Fragment Object
    private Myfragment fg1,fg2,fg3,fg4;
    private FragmentManager fManager;
    private FragmentTransaction fTransaction;
    /**
     * 1) fragmentmanager
     * getFragmentManager()
     *
     * 2) transaction
     *      * getFragmentManager().beginTransaction()
     * 碎片业务对象可以对碎片进行直接处理，比如add  replace / hide  show
     *
     * 3) 创建fragment对象
     *
     * 4) 利用事务员对象的add、replace、hide 、show来决定当前要如何显示这些碎片对象
     *
     * 5）调用commit函数，使设置生效
     */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayout);
		txt_topbar = (TextView) findViewById(R.id.txt_topbar);
        txt_channel = (TextView) findViewById(R.id.txt_channel);
        txt_message = (TextView) findViewById(R.id.txt_message);
        txt_better = (TextView) findViewById(R.id.txt_better);
        txt_setting = (TextView) findViewById(R.id.txt_setting);
        ly_content = (FrameLayout) findViewById(R.id.ly_content);
		fManager = getSupportFragmentManager();//getFragmentManager();

		/*txt_channel.setOnClickListener(this);
	    txt_message.setOnClickListener(this);
	    txt_better.setOnClickListener(this);
	    txt_setting.setOnClickListener(this);*/
        txt_channel.setSelected(true);
        findViewById(R.id.txt_channel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                setSelected();
                txt_channel.setSelected(true);
                if(fg1 == null){
                    fg1 = new Myfragment("1Fragment",0);
                    fTransaction.add(R.id.ly_content,fg1)//.setReorderingAllowed(true)
                            .addToBackStack(null);
                    //fTransaction.replace
                }else{
                    fTransaction.show(fg1);
                }
                fTransaction.commit();
                fTransaction = null;
            }
        });
        findViewById(R.id.txt_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                setSelected();
                txt_setting.setSelected(true);
                if(fg4 == null){
                    fg4 = new Myfragment("4Fragment",1);
                    fTransaction.add(R.id.ly_content,fg4)
                           // .setReorderingAllowed(true)//优化事务中涉及的 fragment 的状态变化，以使动画和过渡正常运行
                           .addToBackStack(null);
                }else{
                    fTransaction.show(fg4);
                }
                fTransaction.commit();
                fTransaction = null;
            }
        });
        findViewById(R.id.txt_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                setSelected();
                txt_message.setSelected(true);
                if(fg2 == null){
                    fg2 = new Myfragment("2Fragment",2);
                    fTransaction.add(R.id.ly_content,fg2)//.setReorderingAllowed(true)
                            .addToBackStack(null);
                }else{
                    fTransaction.show(fg2);
                }
                fTransaction.commit();
                fTransaction = null;
            }
        });
        findViewById(R.id.txt_better).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                setSelected();
                txt_better.setSelected(true);
                if(fg3 == null){
                    fg3 = new Myfragment("3Fragment",3);
                    fTransaction.add(R.id.ly_content,fg3)
                            .setReorderingAllowed(true)
                            .addToBackStack(null);
                }else{
                    fTransaction.show(fg3);
                }
                fTransaction.commit();
                fTransaction = null;
            }
        });
	}

    //
    private void setSelected(){
        txt_channel.setSelected(false);
        txt_message.setSelected(false);
        txt_better.setSelected(false);
        txt_setting.setSelected(false);
    }

    //
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }
}
